<?php
namespace Application\model;

use Cola\Exception;
use Cola\Model;

class ServerModel extends Model
{
    protected $_table= 'ops_servers';

    public $cpid = 0;

    private $where = '';

    /**
     * 构造方法的扩展
     */
    public function _init()
    {
    }

    /**
     * 获取区服列表
     * @param $where
     * @return array
     * @throws Exception\DbException
     */
    public function getServers( $where='1' ): array
    {
        // 获取区服列表
        $sql = "SELECT `sgid`, `sid`, `name`, `frontip`, `frontport`, `ip`, `port`, `is_recommend`, `status`, `open_time`, `tag` FROM `ops_servers` where $where order by `sid` desc";
        $servers = $this->db->sql($sql);
        return $servers;
    }

    /**
     * 获取区服组列表
     * @param $sgids
     * @return array
     * @throws Exception\DbException
     */
    public function getServerGroups($sgids=[]): array
    {
        $where = '1';
        $sgids = array_unique($sgids);
        if (empty($sgids)) return [];
        $where .= " and `id` in (" . implode(',', $sgids) . ")";
        $sql = "select id as sgid, name, sort from `ops_server_groups` where $where order by `sort` desc";
        $groups = $this->db('_backdb')->sql($sql);
        return $groups;
    }

    /**
     * 通过sid获取服务器信息
     * @param $sid
     * @return array
     * @throws Exception\DbException
     */
    public function getServerBySid($sid)
    {
        $sql = "select * from `ops_servers` where `sid`='$sid' ";
        $server = $this->db->row($sql);
        return $server;
    }

    /**
     * 公共方法，获取区服一条数据
     * @param $where
     * @param $order
     * @return array
     * @throws Exception\DbException
     */
    public function getServerInfo($where='1', $order='open_time desc'): array
    {
        $sql = "select * from `ops_servers` where  $where order by $order";
        $server = $this->db->row($sql);
        return $server;
    }
    public function getSidsBySrid($serverWhere=''): array
    {
        if(!$serverWhere) return [];
        $sql = "select sid from `ops_servers` where $serverWhere";
        $servers = $this->db->sql($sql);
        return $servers ? array_column($servers,'sid') : [];
    }

    /**
     * 获取推荐服务器 从推荐服务器中随机选择一个返回
     * @return mixed
     * @throws Exception\DbException
     */
    public function getRecommendServer($where='')
    {
        $sql = "select * from `ops_servers` where {$where} and `is_recommend`='1'";
        $server = $this->db->sql($sql);
        // 随机一个key
        $count = count($server);
        if ($count >= 1) $count = count($server)-1;
        $key = mt_rand(0, $count);
        return $server[$key];
    }

    public function getPage($page, $pageSize=1000, $where='')
    {
        $start = ($page - 1) * $pageSize;
        $sql = "select * from `ops_servers` where {$where} order by `sid` limit $start, $pageSize";
        $servers = $this->db->sql($sql);
        $total = $this->db->count($where, 'ops_servers');
        return [
            'total' => $total,
            'servers' => $servers,
        ];

    }


}
